<template>
  <div class="app-container">
    <el-form ref="filterForm" :inline="true" :model="listQuery" class="filter-container">
      <el-form-item prop="inviteid">
        <el-input v-model="listQuery.inviteid" size="small" placeholder="ID" clearable style="width: 100px;" @keyup.enter.native="handleFilter" />
      </el-form-item>
      <el-form-item prop="name">
        <el-input v-model="listQuery.name" size="small" placeholder="姓名" clearable style="width: 200px;" @keyup.enter.native="handleFilter" />
      </el-form-item>

      <el-form-item>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
        >筛选</el-button>
        <el-button
          size="small"
          @click="resetFilter"
        >重置</el-button>
        <el-button
          size="small"
          type="success"
          icon="el-icon-plus"
          @click="toInviteNew"
        >添加</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      stripe
      fit
      highlight-current-row
    >
      <el-table-column label="ID" width="80" align="center">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="VeevaCode" width="100" align="center">
        <template slot-scope="scope">
          {{ scope.row.veevacode }}
        </template>
      </el-table-column>
      <el-table-column label="类型" width="100" align="center">
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.type === 1"
            effect="plain"
            size="mini"
          >医生</el-tag>
          <el-tag
            v-else-if="scope.row.type === 2"
            effect="plain"
            size="mini"
          >员工</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="手机号码" width="120" align="center">
        <template slot-scope="scope">
          {{ scope.row.tel }}
        </template>
      </el-table-column>
      <el-table-column label="邀请码" width="120" align="center">
        <template slot-scope="scope">
          {{ scope.row.invitecode }}
        </template>
      </el-table-column>
      <el-table-column label="姓名">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="医院">
        <template slot-scope="scope">
          {{ scope.row.hospitalName }}
        </template>
      </el-table-column>
      <el-table-column label="科室">
        <template slot-scope="scope">
          {{ scope.row.department }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="公司">
        <template slot-scope="scope">
          {{ scope.row.company }}
        </template>
      </el-table-column> -->
      <el-table-column label="操作" width="180" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="toInviteEdit(scope.row.id)"
          >编辑</el-button>
          <el-button
            type="danger"
            size="mini"
            @click="removeMeetingInvite(scope.row.id)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :auto-scroll="false" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.size" @pagination="fetchData" />
  </div>
</template>

<script>
import {
  getMeetingInvites,
  removeMeetingInvite
} from '@/api/meetings'
import Pagination from '@/components/Pagination'

export default {
  name: 'MeetingInviteList',
  components: {
    Pagination
  },
  props: {
    meetingId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        meetingid: this.meetingId,
        inviteid: undefined,
        name: undefined,
        page: 1,
        size: 20
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getMeetingInvites(this.listQuery).then(response => {
        // console.log(response)
        this.list = response.data
        this.total = response.total
        this.listLoading = false
      }).catch(err => {
        console.log(err)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.list = []
      this.fetchData()
    },
    resetFilter() {
      this.$refs.filterForm.resetFields()
      this.handleFilter()
    },
    removeMeetingInvite(id) {
      this.$confirm('此操作将永久删除该邀请, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeMeetingInvite({
          inviteid: id
        }).then(response => {
          // console.log(response)
          this.fetchData()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(err => {
          console.log(err)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    toInviteNew() {
      if (this.$route.name === 'MeetingShow') {
        this.$router.push(`/meetings/list/${this.meetingId}/invite/new`)
      } else {
        this.$router.push(`/offline-meetings/list/${this.meetingId}/invite/new`)
      }
    },
    toInviteEdit(id) {
      if (this.$route.name === 'MeetingShow') {
        this.$router.push(`/meetings/list/${this.meetingId}/invite/${id}/edit`)
      } else {
        this.$router.push(`/offline-meetings/list/${this.meetingId}/invite/${id}/edit`)
      }
    }
  }
}
</script>
